package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;

import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class SetmealServicelmpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    //分页查询套餐
    @Override
    public PageResult listStemeal(SetmealPageQueryDTO setmealPageQueryDTO) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO, setmeal);
        try {
            PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
            Page<SetmealVO> page =setmealMapper.listByCondition(setmeal);
            return new PageResult(page.getTotal(), page.getResult());
        } catch (Exception e) {
            throw new BaseException("套餐分页查询失败");
        }

    }

    //删除套餐
    @Override
    public void deleteStemeal(List<Long> ids) {
        for (Long id : ids){
            verifyDelete(id);
        }
        try {
            setmealMapper.deleteByIds(ids);
        } catch (Exception e) {
            throw new BaseException("删除套餐失败");
        }
    }

    //修改套餐
    @Override
    public void updateStemeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        try {
            setmealMapper.update(setmeal);
        } catch (Exception e) {
            log.error("更新套餐失败", e);
            throw new BaseException("更新套餐失败");
        }

    }

    //查询套餐
    @Override
    public SetmealVO selectById(Long id) {
        try {
            //查询套餐基本信息
            Setmeal setmeal = setmealMapper.selectById(id);
            if (Objects.isNull(setmeal)) {
                throw new BaseException("套餐不存在");
            }
            //查询套餐菜品信息
            List<SetmealDish> setmealDishes = setmealDishMapper.selectById(id);

            //组装套餐信息
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal, setmealVO);
            setmealVO.setSetmealDishes(setmealDishes);
            return setmealVO;
        } catch (Exception e) {
            throw new BaseException("查询套餐失败");
        }
    }

    //修改套餐状态
    @Override
    public void updateStemealStatus(int status, Long id) {
        //修改状态
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        try {
            setmealMapper.update(setmeal);
        } catch (Exception e) {
            log.error("更新套餐状态失败", e);
            throw new BaseException("更新套餐状态失败");
        }
    }

    //新增套餐
    @Override
    @Transactional
    public void addStemeal(SetmealDTO setmealDTO) {
        //验证是否存在相同名称
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        List<Setmeal> setmeals = setmealMapper.selectByName(setmeal);
        if (!setmeals.isEmpty()) {
            throw new BaseException("套餐名称已存在");
        }
        try {


            //插入时，回显id
            setmealMapper.insert(setmeal);

            //新增菜品
            List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            //设置套餐id
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
            }
            setmealDishMapper.insert(setmealDishes);


        }catch (Exception e){
            log.error("新增套餐失败", e);
            throw new BaseException("新增套餐失败");
        }
    }

    //用户端
    //查询套餐列表
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        try{
            List<Setmeal> setmeals = setmealMapper.list(setmeal);
            return setmeals;
        }catch (Exception e){
            log.error("查询套餐列表失败", e);
            throw new BaseException("查询套餐列表失败");
        }

    }
    //根据套餐id查询菜品信息
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        try{
            List<DishItemVO> dishItemVOs = setmealMapper.getDishItemBySetmealId(id);
            return dishItemVOs;
        }catch (Exception e){
            log.error("查询菜品信息失败", e);
            throw new BaseException("查询菜品信息失败");
        }

    }


    /*
     * 验证删除条件
     * 1.套餐启售状态，不可删除
     * 不可删除时抛出异常
     * */
    private void verifyDelete(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        if (Objects.equals(setmeal.getStatus(), StatusConstant.ENABLE)) {
            throw new BaseException("该套餐已上架，不可删除");
        }

    }


}
